package com.translate.web.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.response.vo.ResponseModel;
import com.controller.base.AbstractCommonController;
import com.translate.entity.Unit;
import com.jwt.IJwtUtil;
import com.translate.web.param.UnitAddParam;
import com.translate.web.param.UnitUpdateParam;
import com.translate.web.param.contract.IUnitAddParam;
import com.param.impl.PageParam;
import com.translate.web.service.IUnitService;
import com.basic.service.contract.IBaseService;
import com.translate.vo.UnitVO;

@RestController
@RequestMapping("unit")
public class UnitController extends AbstractCommonController<Unit, PageParam> {
	private IUnitService service;
	private IJwtUtil jwtUtil;

	public UnitController(IUnitService service) {
		this.service = service;
	}

	@Override
	protected IBaseService<Unit> getBaseService() {
		return this.service;
	}

	protected <VO> Class<VO> getTarget() {
		return (Class<VO>) UnitVO.class;
	}

	protected long getUserId(HttpServletRequest httpServletRequest) {
		return jwtUtil.getId(httpServletRequest.getHeader(jwtUtil.getJwtConfig().getHeader()));
	}

	/**
	 * 添加
	 * 
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/add")
	public ResponseModel<Integer> insert(@RequestBody @Validated UnitAddParam param,
			HttpServletRequest httpServletRequest) {
		int id = this.service.parseInsert(param, Unit.class);

		return ResponseModel.<Integer>success(id);
	}

	protected long setCreateInfo(IUnitAddParam param, HttpServletRequest httpServletRequest) {
		long userId = this.getUserId(httpServletRequest);
		return userId;
	}

	@Autowired
	public void setJwtUtil(IJwtUtil jwtUtil) {
		this.jwtUtil = jwtUtil;
	}

	/**
	 * 添加
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/update")
	public ResponseModel<Integer> update(@Validated @RequestBody UnitUpdateParam param,
			HttpServletRequest httpServletRequest) {
		int res = this.service.parseUpdate(param, Unit.class);
		return ResponseModel.<Integer>success(res);
	}
}